package com.linuxgt.pattern.observer.scheme1;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * Created by LIUTENG on 2015-03-11.
 */
public class UniversityStudent implements Observer {
    Subject subject;
    File myFile;

    public UniversityStudent(Subject subject, String myFile) {
        this.subject = subject;
        subject.addObserver(this);
        this.myFile = new File(myFile);
    }

    @Override
    public void hearTelephone(String heardMess) {
        try {
            RandomAccessFile out = new RandomAccessFile(myFile, "rw");
            out.seek(out.length());
            byte[] b=heardMess.getBytes();
            out.write(b);
            System.out.print("我是一大学生，");
            System.out.println("我向文件"+myFile.getName()+"写入如下内容");
            System.out.println(heardMess);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println(e.toString());
        }
    }
}
